package com.zzyl.nursing.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }



    /**
     * 修改预约信息
     * 
     * @param id 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Long  id)
    {
        Reservation reservation = this.getById( id);
        if (reservation != null) {
            reservation.setStatus(2);
            return this.updateById(reservation) ? 1 : 0;
        }
        return 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询当天取消预约数量
     */
    @Override
    public AjaxResult cancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //查询当天取消预约数量
        long count = this.count(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getStatus, 2)
                .ge(Reservation::getTime, startTime)
                .lt(Reservation::getTime, endTime));
        return AjaxResult.success(count);
    }

    /**
     * 查询预约时间段预约数量
     */
    @Override
    public AjaxResult countByTime(Long time) {
        //将time转换为localdatetime类型
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<Reservation> list = list(Wrappers.<Reservation>lambdaQuery()
                .ge(Reservation::getTime, startTime)
                .lt(Reservation::getTime, endTime)
                .eq(Reservation::getStatus, 0));
        List<Map<String, Object>> result = new ArrayList<>();
        Map<LocalDateTime, Integer> map = new HashMap<>();
        for (Reservation r : list) {
            if(map.get(r.getTime())!=null){
                map.put(r.getTime(), map.get(r.getTime())-1);
            }
            else {
                map.put(r.getTime(),5);
            }

        }
        for (Map.Entry<LocalDateTime, Integer> entry : map.entrySet()) {
            Map<String, Object> m = new HashMap<>();
            m.put("time", entry.getKey());
            m.put("count", entry.getValue());
            result.add(m);
        }
        return AjaxResult.success(result);
    }
    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
//        Reservation reservation = new Reservation();
//        BeanUtils.copyProperties(reservationDto, reservation);
//        reservation.setTime(LocalDateTimeUtil.of(reservation.getTime()));
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

}
